import fasttext
import numpy as np

from util import remove_traditional_chinese

fasttext.FastText.eprint = lambda x: None
model = fasttext.load_model('D:/Users/fanyf/Downloads/aiModel/fastText/cc.zh.300.bin')

# r = model.get_analogies("P-3","双垂尾","护卫舰")
# print(r)
# result = remove_traditional_chinese([item[1] for item in r])


def get_word_vector(word):

    word_vector = model.get_word_vector(word)

    return word_vector


def cos_sim(vector_a, vector_b):
    """
    计算两个向量之间的余弦相似度
    :param vector_a: 向量 a
    :param vector_b: 向量 b
    :return: sim
    """
    vector_a = np.array(vector_a)

    vector_b = np.array(vector_b)
    num = float(vector_a * vector_b.T)
    denom = np.linalg.norm(vector_a) * np.linalg.norm(vector_b)
    cos = num / denom
    sim = 0.5 + 0.5 * cos
    return sim



if __name__ == '__main__':
    print(cos_sim("P-3","双垂尾",))